<template>
  <div class="container">
    <el-card class="box-card">
      <el-row style="height:60px">
        <span style="color:pink;font-size: 12px">说明：目前支持学科和关键字条件筛选</span>
        <el-button @click="addQuestion" style="float: right" type="success" icon="el-icon-edit" size="small">新增试题</el-button
        >
      </el-row>

      <el-row>
        <!-- 表单 -->
        <el-form size="small" label-position="right" label-width="80px">
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select placeholder="请选择" v-model="params.subjectID" style="width:100%">
                <el-option
                  v-for="item in subjectList" :key="item.id"
                  :label="item.subjectName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select placeholder="请选择" v-model="params.catalogID" style="width:100%">
                <el-option v-for="item in catalogList" :key="item.id"
                :label="item.directoryName"
                :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select placeholder="请选择" v-model="params.tags" style="width:100%">
                <el-option v-for="item in tagList" :key="item.id"
                :label="item.tagName"
                :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="params.keyword" placeholder="根据题干搜索"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select placeholder="请选择" v-model="params.questionType" style="width:100%">
                <el-option v-for="item in questionTypeList" :key="item.value" :label="item.label"
                :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select placeholder="请选择" v-model="params.difficulty" style="width:100%">
                <el-option v-for="item in difficultyList" :key="item.value" :label="item.label"
                :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select placeholder="请选择" v-model="params.direction" style="width:100%">
                <el-option v-for="item in directionList" :key="item" :label="item" :value="item" ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select placeholder="请选择" v-model="params.creatorID" style="width:100%">
                <el-option v-for="item in userList" :key="item.id" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="params.remarks"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="params.shortName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select style="width:49%;margin-right:2%" placeholder="请选择市" v-model="params.province">
                <el-option v-for="item in provincesList" :key="item" :label="item" :value="item"></el-option>
              </el-select>
              <el-select style="width:49%" placeholder="请选择区" v-model="params.city">
                <el-option v-for="item in citysList" :key="item" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="float: right">
              <el-button @click="remove">清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <el-row>
        <el-col :span="24"
          ><div class="grid-content bg-purple-dark">
            <i class="el-icon-info"></i><span>数据一共{{total}}条</span>
          </div></el-col
        >
      </el-row>

      <!-- 表格 -->
      <el-table :data="questionsList" style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="120"> </el-table-column>
        <el-table-column prop="subject" label="学科"> </el-table-column>
        <el-table-column prop="catalog" label="目录"> </el-table-column>
        <el-table-column label="题型">
          <template #default="{row}">
            {{ formatType(row.questionType) }}
          </template>
        </el-table-column>
        <el-table-column width="250" prop="question" label="题干">
          <template #default="{row}">
            {{ formatQuestion(row.question) }}
          </template>
        </el-table-column>
        <el-table-column width="180" prop="addDate" label="录入时间">
          <template #default="{row}">
            {{ formatDate(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column label="难度">
          <template #default="{row}">
            {{ formatDifficulty(row.difficulty) }}
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人"> </el-table-column>
        <el-table-column width="195" prop="address" label="操作">
          <template #default={row}>
            <el-tooltip content="预览" placement="bottom" effect="light" :open-delay="800">
              <el-button plain @click="openDialog(row)" type="primary" icon="el-icon-view" circle></el-button>
            </el-tooltip>
            <el-tooltip content="修改" placement="bottom" effect="light" :open-delay="800">
              <el-button plain @click="edit(row.id)" type="success" icon="el-icon-edit" circle></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="bottom" effect="light" :open-delay="800">
              <el-button plain @click="del(row.id)" type="danger" icon="el-icon-delete" circle></el-button>
            </el-tooltip>
            <el-tooltip content="加入精选" placement="bottom" effect="light" :open-delay="800">
              <el-button plain @click="choice(row.id)" type="warning" icon="el-icon-check" circle></el-button>
            </el-tooltip>
          </template>

        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        style="text-align: right; margin: 20px 5px"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="5"
        :current-page="query.page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>

    </el-card>

    <!-- 引入预览组件 -->
    <questions-preview ref="detailDialog" :is-edit='isEdit' :dialogVisible='showPreviewDialog' @close-dialog='closePreviewDialog'  ></questions-preview>

  </div>
</template>

<script>
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
import { list as getSubjectList } from '@/api/hmmm/subjects.js'
import dayjs from 'dayjs'
import QuestionsPreview from '@/module-hmmm/components/question-preview'
import { list as getDirectoryList } from '@/api/hmmm/directorys.js'
import { list as getTagList } from '@/api/hmmm/tags.js'
// 难度 题型。。。
import { questionType, difficulty, direction } from '@/api/hmmm/constants.js'
// 城市
// import citys from '@/api/hmmm/citys.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
// 录入人 用户列表
import { list as getUserList } from '@/api/base/users.js'

export default {
  components: {
    QuestionsPreview
  },
  data () {
    return {
      isEdit: true,
      dialogVisible: false,
      showPreviewDialog: false,
      id: '',
      questionsList: [],
      // 基础题库列表参数
      query: {
        page: 1,
        pagesize: 5 // 每页多少条
      },
      // subjectID: '', // 学科
      total: 0,
      subjectList: [], // 学科列表
      catalogList: [], // 目录列表
      tagList: [], // 标签列表、
      questionTypeList: questionType, // 试题类型列表
      difficultyList: difficulty, // 难度
      directionList: direction, // 方向
      provincesList: [], // 城市
      citysList: [], // 城市
      userList: [], // 用户
      params: {
        subjectID: '', // 学科
        catalogID: '', // 目录
        tags: '', // 标签
        keyword: '', // 关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 省份
        city: '' // 城市
      }
    }
  },
  mounted () {
    this.getList(this.query)
    this.getSubject() // 获取学科下拉列表
    // this.getDirectory()
    // this.directionList = direction
    this.getUser() // 录入人
    // 获取城市数组
    const province = provinces()
    this.provincesList = province
  },
  methods: {
    // 获取试题列表
    async getList (val) {
      const res = await list(val)
      console.log(res)
      this.questionsList = res.data.items
      this.total = res.data.counts
      console.log(this.total)
    },
    // 题型格式化
    formatType (value) {
      const MAP = {
        1: '单选',
        2: '多选',
        3: '简答'
      }
      return MAP[value]
    },
    // 题干格式化
    formatQuestion (value) {
      return value.replace(/<.*?>/ig, '')
    },
    // 难度格式化
    formatDifficulty (value) {
      const MAP = {
        1: '简单',
        2: '一般',
        3: '困难'
      }
      return MAP[value]
    },
    // 时间格式化
    formatDate (value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    },
    // 分页 当前页
    handleCurrentChange (page) {
      console.log(page)
      this.query.page = page
      this.getList(this.query)
    },
    // 分页 每页几条
    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      this.getList(this.query)
    },
    // 学科下拉框 获取列表
    async  getSubject () {
      const res = await getSubjectList({ page: 1, pagesize: 50 })
      console.log(res)
      this.subjectList = res.data.items
    },
    // 获取目录列表
    async getDirectory (val) {
      // 获取目录列表
      const res1 = await getDirectoryList(val)
      console.log(res1)
      // return res1.data.items
      this.catalogList = res1.data.items
    },
    // 获取标签列表
    async getTag (val) {
      const res1 = await getTagList(val)
      console.log(res1)
      this.tagList = res1.data.items
    },
    // 新增跳转
    addQuestion () {
      this.$router.push('/questions/new')
    },
    // 修改跳转
    edit (id) {
      // console.log(id)
      this.$router.push({
        path: '/questions/new',
        query: { id }
      })
    },
    // 删除
    del (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList(this.query)
      }).catch(() => {

      })
    },
    // 加入精选
    choice (id) {
      console.log(id)
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        type: 'info'
      }).then(async () => {
        // 加入精选接口 1加入0移出
        await choiceAdd({ id, choiceState: 1 })
        // 从基础题库删除
        // await remove({ id })
        this.$message({
          type: 'success',
          message: '加入精选成功!'
        })
        this.getList(this.query)
        // 跳转到精选题库
        this.$router.push('/questions/choice')
      }).catch(() => {

      })
    },
    // 预览
    // async preview (id) {
    //   console.log(id)
    //   this.dialogVisible = true
    //   this.id = id
    // },
    // 预览题目详情
    openDialog (data) {
      this.showPreviewDialog = true
      this.$refs.detailDialog.getSelectDetail(data)
    },
    closePreviewDialog () {
      this.showPreviewDialog = false
    },
    // 点x关闭弹框
    closeDialog () {
      this.dialogVisible = false
    },
    // 获取录入人
    async getUser () {
      const res = await getUserList(this.query)
      console.log(res)
      this.userList = res.data.list
    },
    // 搜索
    search () {
      this.getList({ ...this.query, ...this.params })
    },
    // 清除
    remove () {
      this.params = {
        subjectID: '', // 学科
        catalogID: '', // 目录
        tags: '', // 标签
        keyword: '', // 关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 省份
        city: '' // 城市
      }
      this.getList(this.query)
    }
  },
  watch: {
    'params.subjectID' (val) {
      console.log(val)
      // 目录列表接口
      this.getDirectory({ subjectID: val })
      // 标签列表接口
      this.getTag({ subjectID: val })
    },
    'params.province' (val) {
      this.citysList = citys(val)
    }
  }
}
</script>

<style scoped lang="less">
.container{
  margin: 10px 8px 0 8px;
}
.bg-purple-dark {
  background: #f4f4f5;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
  padding: 0 16px;
  line-height: 36px;
  color: #909399;
  margin-bottom: 16px;
  i {
    margin-right: 6px;
  }
  span {
    font-size: 13px;
  }

}
</style>
